package com.lab.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lab.dao.CourseDao;
import com.lab.dao.impl.CourseDaoImpl;
import com.lab.po.PageBean;

/**
 * Servlet implementation class CouPageAjax
 */
@WebServlet("/couPageAjax")
public class CouPageAjax extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CouPageAjax() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int currentPage=1;
		if(request.getParameter("currentPage")!=null){
			currentPage=Integer.parseInt(request.getParameter("currentPage"));
		}
		//ajax请求
		CourseDao cd=new CourseDaoImpl();
		PageBean pagebean=new PageBean();
		pagebean.setCurrentPage(currentPage);

		String cou_name=request.getParameter("cou_name");

		Map map=new HashMap();
		if(cou_name!=null && !cou_name.equals("")){
			map.put("cou_name", "%"+cou_name+"%");
		}
		pagebean.setCountRecords(cd.countCouByCondition(map));
		if(currentPage==1){
			pagebean.setPageUp(1);
		}else{
			pagebean.setPageUp(currentPage-1);
		}
		if(currentPage==pagebean.getTotalPage()){
			pagebean.setPageDown(pagebean.getTotalPage());
		}else{
			pagebean.setPageDown(currentPage+1);
		}
		String pb="<tr><td colspan=\"8\">当前页："+pagebean.getCurrentPage()+"/"+pagebean.getTotalPage()
	 			 +"&nbsp;&nbsp;<a  onclick=\"query(1)\">首页</a>&nbsp;&nbsp;<a  onclick=\"query("+pagebean.getPageUp()+")\">上一页</a>&nbsp;&nbsp;<a  onclick=\"query("+pagebean.getPageDown()+")\">下一页</a>"
	 			 +"&nbsp;&nbsp;<a  onclick=\"query("+pagebean.getTotalPage()+")\">末页</a></td></tr>";
		System.out.println(pb);
		response.getWriter().write(pb);
	}

}
